﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class Popup_QuickPVP : Popup_Base
{
	const string REMAIN_TIMER_FORMAT = "{0:00}:{1:00}:{2:00}";

	public KinaTemplate kinaTemplate;
	public GemTemplate gemTemplate;
	public SoulStoneTemplate soulStoneTemplate;
	public ItemTemplate itemTemplate;
	public UILabel remainTimeLabel;
	public GameObject[] starBadgeArray;

	TimeSpan timeGap;
	DateTime targetTime;
	DateTime endTime;

	IEnumerator RemainTimeTimer()
	{
		while(true)
		{
			timeGap = endTime - DateTime.Now;

			if(timeGap.Hours < 0)
				break;

			remainTimeLabel.text = string.Format(REMAIN_TIMER_FORMAT, timeGap.Hours, timeGap.Minutes, timeGap.Seconds);
			yield return new WaitForSeconds(1.0f);
		}
	}

    public override void AwakeInit()
  	{
		targetTime = DateTime.Now.AddDays(1);
		endTime = new DateTime(targetTime.Year, targetTime.Month, targetTime.Day, 0, 0, 0);
		StartCoroutine(RemainTimeTimer());

		// 일일 침략을 1회도 안 했을 경우 (우선 rewardId은 quickPvpInfo가 null일 경우 1번이 default)
		if (UserData.Instance.userModel.quickPvpInfo == null)
		{
			NGUITools.SetActive(kinaTemplate.gameObject, true);
			kinaTemplate.value2Label.text = GetInvadeReward(1).value2.ToString();
		}
		// 일일 침략을 1회 이상 했을 경우
		else
		{
			int _rewardId = UserData.Instance.userModel.quickPvpInfo.rewardId;
			_rewardId = (_rewardId == 1 ? 2 : 1);

			//TODO: 우선 MS8에서는 보상을 키나로 고정시킴
			_rewardId = 1;

			DInvadeReward _invadeReward = GetInvadeReward(_rewardId);

			switch(_invadeReward.rewardType)
			{
			case 1: // 키나
				NGUITools.SetActive(kinaTemplate.gameObject, true);
				kinaTemplate.value2Label.text = _invadeReward.value2.ToString();
				break;

			case 2: // 잼
				NGUITools.SetActive(gemTemplate.gameObject, true);
				gemTemplate.value2Label.text = _invadeReward.value2.ToString();
				break;

			case 3: // 봉혼석
				//NGUITools.SetActive(soulStoneTemplate.gameObject, true);
				break;

			case 4: // 아이템
				//NGUITools.SetActive(itemTemplate.gameObject, true);
				//itemTemplate.value1Label.text = _invadeReward.value1.ToString();
				//itemTemplate.value2Label.text = _invadeReward.value2.ToString();
				break;
			}

			updateStarBadge(UserData.Instance.userModel.quickPvpInfo.victoryCount);
		}
    }

	DInvadeReward GetInvadeReward(int rewardId)
	{
		foreach (DInvadeReward _ir in GameData.Inst.invadeReward)
		{
			if (rewardId == _ir.rewardID)
				return _ir;
		}
		return null;
	}

    public override void BackButton()
    {
        PopupManager.PopPopup();
    }

	public override void ButtonTouch(Collider collider)
	{
		switch (collider.gameObject.name)
		{
		case "Button_Invade":
			if (m_MessageRecieaver_OK != null && m_Message_OK != null)
				m_MessageRecieaver_OK.SendMessage(m_Message_OK);

			BackButton();
			break;

		case "Button_Cancel":
			if (m_MessageRecieaver_NO != null && m_Message_NO != null)
				m_MessageRecieaver_NO.SendMessage(m_Message_NO);

			BackButton();
			break;

		case "Button_Back":
			BackButton();
			break;
		}
	}

	public void updateStarBadge(int starCount)
	{
		for (int i = 0; i < starCount; i++)
		{
			NGUITools.SetActive(starBadgeArray[i], true);
		}
	}
}